package com.fruit.web.vo.common;

import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.fruit.hibernate.dao.support.Page;
import com.fruit.service.IOrderService;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Order;
import com.fruit.web.constant.SpringBeanConstants;


public class OrderPage implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224760L;
	private Page<Order,Long> page = new Page<Order,Long>();
	private SelectItem[] pageItems = null ;
	private Integer pageSize ;
	private Integer pageNo ;
	private Order param = null;//except status
	private List<String> status = new ArrayList<String>(); 
	private java.sql.Timestamp fromTime = null;
	private java.sql.Timestamp toTime = null;
	public Page<Order, Long> getPage() {
		return page;
	}
	public void setPage(Page<Order, Long> page) {
		this.page = page;
	}
	public void getPageDatas() {//产品关联的所有相关咨询
		Page<Order,Long> curPage = new Page<Order,Long>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(10);
		}
		curPage = this.getData(pPageNo, pPageSize, this.getParam());
		SelectItem[] tempPageItem = new SelectItem[(int)curPage.getTotalPageCount()];
		for (int j=0;j<tempPageItem.length;j++) {
			tempPageItem[j]= new SelectItem(new Integer(j+1),String.valueOf(j+1));
		}
		this.setPageItems(tempPageItem);
		this.setPage(curPage);
	}
	public SelectItem[] getPageItems() {
		return pageItems;
	}
	private static SelectItem[] pageSizeItems = {
		new SelectItem(new Long(5),"5"),
		new SelectItem(new Long(10),"10"),
	};
	public SelectItem[] getPageSizeItems() {
		return pageSizeItems;
	}

	public void setPageSizeItems(SelectItem[] pageSizeItems) {
		OrderPage.pageSizeItems = pageSizeItems;
	}
	public void setPageItems(SelectItem[] pageItems) {
		this.pageItems = pageItems;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	public Order getParam() {
		return param;
	}
	public void setParam(Order param) {
		this.param = param;
	}
	
	public List<String> getStatus() {
		return status;
	}
	public void setStatus(List<String> status) {
		this.status = status;
	}
	public java.sql.Timestamp getFromTime() {
		return fromTime;
	}
	public void setFromTime(java.sql.Timestamp fromTime) {
		this.fromTime = fromTime;
	}
	public java.sql.Timestamp getToTime() {
		return toTime;
	}
	public void setToTime(java.sql.Timestamp toTime) {
		this.toTime = toTime;
	}
	public OrderPage(Integer pageNo, Integer pageSize, Order param) {
		super();
		this.pageSize = pageSize;
		this.pageNo = pageNo;
		this.param = param;
		
	}
	
	//所有的查询均根据param的值确定
	private Page<Order,Long> getData(int pageNo, int pageSize, Order param) {// param is statuscode and customer
		Page<Order,Long> resultPage = new Page<Order,Long>();
		IOrderService<Order,Long> orderService = (IOrderService<Order,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.ORDER_SERVICE_BEAN_NAME);
		orderService.setEntityClass(Order.class);
		orderService.setPKClass(Long.class);
		Criteria criteria = orderService.createCriteria(null);
		if (null != param.getOrderId() && !("".equals(param.getOrderId()))) {
			criteria.add(Restrictions.eq("orderId", param.getOrderId()));
		}
		if (null != this.getStatus() && !(this.getStatus().isEmpty())) {
			criteria.add(Restrictions.in("status", status));
		}
		if (null != param.getConsigneeProvince() && !("".equals(param.getConsigneeProvince()))) {
			criteria.add(Restrictions.eq("consigneeProvince", param.getConsigneeProvince()));
		}
		if (null != param.getConsigneeCity() && !("".equals(param.getConsigneeCity()))) {
			criteria.add(Restrictions.eq("consigneeCity", param.getConsigneeCity()));
		}
		if (null != param.getConsigneeCounty() && !("".equals(param.getConsigneeCounty()))) {
			criteria.add(Restrictions.eq("consigneeCounty", param.getConsigneeCounty()));
		}
		if (null != param.getDeliveryId() && !("".equals(param.getDeliveryId()))) {
			criteria.add(Restrictions.eq("deliveryId", param.getDeliveryId()));
		}
		if (null != param.getPaymentType() && !("".equals(param.getPaymentType()))) {
			criteria.add(Restrictions.eq("paymentType", param.getPaymentType()));
		}
		if (null != param.getPaymentId() && !("".equals(param.getPaymentId()))) {
			criteria.add(Restrictions.eq("paymentId", param.getPaymentId()));
		}
		if (null != param.getPaymentTypeId() && !("".equals(param.getPaymentTypeId()))) {
			criteria.add(Restrictions.eq("paymentTypeId", param.getPaymentTypeId()));
		}
		if (null != param.getInvoiceType() && !("".equals(param.getInvoiceType()))) {
			criteria.add(Restrictions.eq("invoiceType", param.getInvoiceType()));
		}
		if (null != this.getFromTime() && null != this.getToTime() && this.getFromTime().before(this.getToTime())) {
			criteria.add(Restrictions.ge("orderGenerateTime", this.fromTime));
			criteria.add(Restrictions.le("orderGenerateTime", this.toTime));
		}
		if (null != param.getCustomer() && (null != param.getCustomer().getCustomerId() && !("".equals(param.getCustomer().getCustomerId())))) {
			criteria.createCriteria("customer").add(Restrictions.eq("customerId", param.getCustomer().getCustomerId()));
		}
		resultPage = orderService.pagedQuery(criteria, pageNo, pageSize); 
		return resultPage;
	}
}
